/**   
* @Title: UsersForeachHandler.java
* @Package gov.pbc.gx.hc.sa.uflo
* @Description: TODO(用一句话描述该文件做什么)
* @author WLF
* @date 2015-11-9 下午4:28:49
* @version V1.0
*/ 
package gov.pbc.nn.kjgl.joinjrfwygl.handle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.business.IUser;
import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.env.Context;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.process.handler.ForeachHandler;

import gov.pbc.nn.core.bdf.entity.User;
import gov.pbc.nn.core.utils.SecurityUtils;
import gov.pbc.nn.kjgl.Constants;
import gov.pbc.nn.kjgl.joinjrfwygl.entity.HandleDept;
import gov.pbc.nn.kjgl.task.dao.TaskDao;
import gov.pbc.nn.kjgl.xkyyh.dao.NewBankDao;
import gov.pbc.nn.kjgl.xkyyh.entity.BackTrain;
import gov.pbc.nn.kjgl.xkyyh.entity.TrainDept;

/** 
 * @ClassName: UsersForeachHandler
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author WLF
 * @date 2015-11-9 下午4:28:49
 * 
 */
@Component("joinjrfwygl.trainHandlersForeachHandler")
public class TrainHandlersForeachHandler implements ForeachHandler {

	/** (非 Javadoc) 
	 * Title: handle
	 * Description:
	 * @param arg0
	 * @param arg1
	 * @return
	 * @see com.bstek.uflo.process.handler.ForeachHandler#handle(com.bstek.uflo.model.ProcessInstance, com.bstek.uflo.env.Context)
	 */
	@Resource(name=ProcessClient.BEAN_ID)
	private ProcessClient processClient;

	@Autowired
	private NewBankDao bankDao;
	
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Object> handle(ProcessInstance processInstance, Context context) {
		// TODO Auto-generated method stub
		String businessId = processInstance.getBusinessId();
		String hql = "from " + HandleDept.class.getName() + " where joinId=:id and invalid=false and opinion=0";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", businessId);
		List<HandleDept> handleDepts =this.bankDao.query(hql, params);
		Collection<Object> handles = new ArrayList<Object>();
		for (HandleDept d : handleDepts) {
			Map<String, Object> handle=new HashMap<String, Object>();				
			handle.put("handleDeptId", d.getId());
			List<IUser> users=SecurityUtils.getUserByRoleForDept(d.getDeptId(), Constants.ROLE_CZ);
			List<String> usernames=new ArrayList<String>();
			for (IUser user : users) {
				usernames.add(user.getUsername());
			}
			handle.put("trainCz", usernames);
			handles.add(handle);
		}
		return handles;
	}

}
